package com.mock.ems.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mock.ems.common.DistributionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;

import java.util.Map;

/**
 * Created by MOCK on 2017/8/17.
 */
public class OrderSubscriber extends JedisPubSub {

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        logger.info("subscrib to channel : {} succeed!",channel);
        super.onSubscribe(channel, subscribedChannels);
    }

    private static final Logger logger = LoggerFactory.getLogger(OrderSubscriber.class);

    @Override
    public void onMessage(String channel, String message) {
        super.onMessage(channel, message);
        try {

            Map<String,Object> orderMap = JSON.parseObject(message,new TypeReference<Map<String,Object>>(){});
            logger.info("orderMap:{}",orderMap);
            DistributionHandler.handle(orderMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
